
#include"SeqList.h"

void SLInit(SL * ps)
{
	ps->arr = NULL; 
	ps->capaCity = 0;
	ps->size = 0;
}

void SLDestry(SL* ps)
{
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = ps->capaCity = 0;
}
void SLCheckCapacity(SL* ps)
{
	if (ps->size == ps->capaCity)
	{
		int newCapacity = ps->size == 0 ? 4 : 2 * ps->size;
		SLDataType* tamp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));
		if (tamp == NULL)
		{
			perror("releak failed");
			exit(1);
		}
		ps->arr = tamp;
		ps->capaCity = newCapacity;
	}
}
  void PlusPushBack(SL* ps, SLDataType x)
{
	  assert(ps);
	  SLCheckCapacity(ps);
	  ps->arr[ps->size++] = x;
}

  void PlusPushFront(SL* ps, SLDataType x)
  {
	  assert(ps);
	  SLCheckCapacity(ps);
	  for (int i = ps->size; i > 0; i--)
	  {
		  ps->arr[i] = ps->arr[i - 1];
	  }
	  ps->arr[0] = x;
	  ps->size++;
  }
  
  void PlusPopBack(SL* ps)
  {
	  assert(ps);
	  SLCheckCapacity(ps);
	  ps->size--;
  }

  void PLusPopFront(SL* ps)
  {
	  assert(ps);
	  assert(ps->size);
	  SLCheckCapacity(ps);
	  for (int i = 0; i < ps->size - 1; i++)
	  {
		  ps->arr[i] = ps->arr[i + 1];
	  }
	  ps->size--;
  }

  /*void SLPrint(SL sl)
  {
	  for (int i = 0; i < sl.size; i++)
	  {
		  printf("%d", sl.arr[i]);
	  }
	  printf("/n");
  }*/

  void SLInsert(SL* ps, int pos, SLDataType x)
  {
	  assert(ps);
	  assert(pos >= 0 && pos < ps->size);
	  SLCheckCapacity(ps);
	  for (int i = ps->size; i > pos; i--)
	  {
		  ps->arr[i] = ps->arr[i - 1];
	  }
	  ps->arr[pos] = x;
	  ps->size++;
  }

  void SLErase(SL* ps, int pos)
  {
	  assert(ps);
	  assert(pos >= 0 && pos < ps->size);

	  for (int i = pos; i < ps->size - 1; i++)
	  {
		  ps->arr[i] = ps->arr[i + 1];
	  }
	  ps->size--;
  }